package com.bocek.chakmapoint.view.menubar;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import com.bocek.chakmapoint.controller.SlideEditController;
import com.bocek.chakmapoint.view.MainView;
import com.bocek.chakmapoint.view.slide.SlideView;
import com.bocek.chakmapoint.view.slideshow.SlideshowView;
import com.bocek.chakmapoint.view.sort.SlideSortView;
import com.bocek.chakmapoint.view.toolbar.ToolbarView;

import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

/**
 * Panel for drawing current slide contents.
 * 
 * @author  Baris Sencan
 * @version 1.3
 */
public class MenubarView extends JPanel {
	private static final long serialVersionUID = -989237816052087436L;
	private static final MenubarView instance = new MenubarView();
	private static JLabel lblNo;
	
	public static MenubarView getInstance() {
		return instance;
	}

	/**
	 * Create the panel.
	 * 
	 * @requires Nothing.
	 * @modifies this
	 * @effects  Nothing.
	 */
	private MenubarView() {
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(null);
		
		lblNo = new JLabel("0 / 0");
		lblNo.setBounds(12, 296, 61, 16);
		add(lblNo);
		
		JButton btnSelectTool = new JButton("Select Tool");
		btnSelectTool.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SlideEditController.getInstance().cancelButtonPressed();
				( (CardLayout) ToolbarView.getInstance().getLayout()).show(
						ToolbarView.getInstance(), "selectView");
			}
		});
		btnSelectTool.setBounds(2, 6, 117, 26);
		add(btnSelectTool);
		
		JButton btnNewButton = new JButton("Line Tool");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideEditController.getInstance().createZigZagButtonPressed();
				( (CardLayout) ToolbarView.getInstance().getLayout()).show(
						ToolbarView.getInstance(), "zigzaglineView");
			}
		});
		btnNewButton.setBounds(2, 32, 117, 26);
		add(btnNewButton);
		
		JButton btnRectangleTool = new JButton("Rectangle Tool");
		btnRectangleTool.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideEditController.getInstance().createRectangleButtonPressed();
				( (CardLayout) ToolbarView.getInstance().getLayout()).show(
						ToolbarView.getInstance(), "rectangleView");
			}
		});
		btnRectangleTool.setBounds(2, 58, 117, 26);
		add(btnRectangleTool);
		
		JButton btnOvalTool = new JButton("Oval Tool");
		btnOvalTool.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideEditController.getInstance().createOvalButtonPressed();
				( (CardLayout) ToolbarView.getInstance().getLayout()).show(
						ToolbarView.getInstance(), "ovalView");
			}
		});
		btnOvalTool.setBounds(2, 84, 117, 26);
		add(btnOvalTool);
		
		JButton btnTextTool = new JButton("Text Tool");
		btnTextTool.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideEditController.getInstance().createTextButtonPressed();
				( (CardLayout) ToolbarView.getInstance().getLayout()).show(
						ToolbarView.getInstance(), "textView");
			}
		});
		btnTextTool.setBounds(2, 110, 117, 26);
		add(btnTextTool);
		
		JButton btnFront = new JButton("Bring to Front");
		btnFront.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SlideEditController.getInstance().moveToFrontButtonPressed();
			}
		});
		btnFront.setBounds(2, 146, 117, 26);
		add(btnFront);
		
		JButton btnBack = new JButton("Send to Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideEditController.getInstance().moveToBackButtonPressed();
			}
		});
		btnBack.setBounds(2, 172, 117, 26);
		add(btnBack);
		
		JButton btnPrevious = new JButton("Previous Slide");
		btnPrevious.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideEditController.getInstance().previousSlide();
				updateLabels();
			}
		});
		btnPrevious.setBounds(2, 208, 117, 26);
		add(btnPrevious);
		
		JButton btnNextButton = new JButton("Next Slide");
		btnNextButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideEditController.getInstance().nextSlide();
				updateLabels();
			}
		});
		btnNextButton.setBounds(2, 234, 117, 26);
		add(btnNextButton);
		
		JButton btnNewSlide = new JButton("New Slide");
		btnNewSlide.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideEditController.getInstance().createNewSlideButtonPressed();
				updateLabels();
			}
		});
		btnNewSlide.setBounds(2, 260, 117, 26);
		add(btnNewSlide);
		
		JButton btnAddTitle = new JButton("Add Title");
		btnAddTitle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideEditController.getInstance().addTitle();
			}
		});
		btnAddTitle.setBounds(2, 322, 117, 26);
		add(btnAddTitle);
		
		JButton btnToggleNumber = new JButton("Toggle Number");
		btnToggleNumber.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SlideEditController.getInstance().toggleShowNumber();
			}
		});
		btnToggleNumber.setBounds(2, 348, 117, 26);
		add(btnToggleNumber);
		
		JButton btnSort = new JButton("Sort");
		btnSort.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainView.getInstance().setVisible(false);
				SlideSortView.getInstance().updateList();
				SlideSortView.getInstance().setVisible(true);
			}
		});
		btnSort.setBounds(2, 384, 117, 26);
		add(btnSort);
		
		JButton btnSlideshow = new JButton("Slideshow");
		btnSlideshow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainView.getInstance().setVisible(false);
				SlideshowView.getInstance().setVisible(true);
			}
		});
		btnSlideshow.setBounds(2, 408, 117, 26);
		add(btnSlideshow);
	}
	
	public void updateLabels()  {
		updateTitleLabel();
		updateSlideNoLabel();
	}
	
	public void updateTitleLabel() {
		MainView.getInstance().setTitleLabel(
				SlideEditController.getInstance().getCurrentSlideName());
	}
	
	public void updateSlideNoLabel() {
		SlideEditController sec = SlideEditController.getInstance();
		
		lblNo.setText(sec.getCurrentSlideNo() + " / " + sec.getSlideCount());
	}
}
